package com.ssy.wireless.web.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ssy.wireless.entity.Enterprise;
import com.ssy.wireless.entity.LocalResource;
import com.ssy.wireless.util.RespVo;
import com.ssy.wireless.web.service.EnterpriseService;
import com.ssy.wireless.web.service.LocalResourceService;
import com.ssy.wireless.web.vo.req.LocalResourceQueryVo;
import com.ssy.wireless.web.vo.resp.EchartsPieDataVo;
import com.ssy.wireless.web.vo.resp.EchartsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ssy.wireless.util.RespVo.success;

/**
 * Created by cofey on 16/11/20.
 */
@RestController
@RequestMapping("/lr")
public class LocalResourceController extends AbstractController {
    @Autowired
    private LocalResourceService localResourceService;
    @Autowired
    private EnterpriseService enterpriseService;

    @GetMapping("/")
    public ModelAndView list() {
        return new ModelAndView("lr/list");
    }

    @PostMapping("/content")
    public ModelAndView content(
            @RequestParam(value = "pageIndex", defaultValue = "1", required = false) int pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) int pageSize,
            LocalResourceQueryVo vo
    ) {
        Assert.isTrue(pageSize < 200, "每页显示条数不能大于200");
        Page<LocalResource> page = PageHelper.offsetPage(pageIndex - 1, pageSize);
        localResourceService.find(vo.queryParams());
        return new ModelAndView("lr/content", result(page));
    }

    @GetMapping("/statistics")
    public ModelAndView statistics() {
        List<Enterprise> ents = enterpriseService.find(null);
        Map<String, Object> results = new HashMap<>();
        results.put("ents", ents);
        return new ModelAndView("lr/statistics", results);
    }

    @PostMapping("/statistics/content")
    public RespVo<EchartsVo> statisticsContent(@RequestParam(value = "area", required = false) String area,
                                               @RequestParam(value = "entId", required = false) Long entId) {
        List<String> legends = new ArrayList();
        legends.add("视频");
        legends.add("文档");
        legends.add("图片");
        legends.add("其他");
        List<EchartsPieDataVo> series = new ArrayList();
        Map<String, Object> params = new HashMap<>();
        params.put("entId", entId);
        params.put("area", area);
        List<LocalResource> statistics = localResourceService.findStatistics(params);
        statistics.stream().forEach(lr -> {
            series.add(new EchartsPieDataVo().setName(legends.get(lr.getType())).setValue(null != lr.getResCount() ? lr.getResCount().toString() : "0"));
        });
        for (String legend : legends) {
            boolean flag = false;
            for (EchartsPieDataVo vo : series) {
                if (vo.getName().equals(legend)) {
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                series.add(new EchartsPieDataVo().setName(legend).setValue("0"));
            }
        }
        return success(EchartsVo.build().createTitle("本地化资源统计").createLegend(legends).createSeries(series, "", ""));
    }
}
